{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 对Song做基于user的协同过滤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "#load数据（用户和物品索引，以及倒排表）\n",
    "import pickle as pk\n",
    "\n",
    "#稀疏矩阵，打分表\n",
    "import scipy.io as sio\n",
    "import os\n",
    "\n",
    "import scipy.spatial.distance as ssd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "temp_data = './temp_data/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#读取预备数据\n",
    "users_index = pk.load(open(temp_data + \"users_index.pkl\", 'rb'))\n",
    "items_index = pk.load(open(temp_data + \"items_index.pkl\", 'rb'))\n",
    "user_2_items = pk.load(open(temp_data + \"user_2_items.pkl\", 'rb'))\n",
    "item_2_users = pk.load(open(temp_data + \"item_2_users.pkl\", 'rb'))\n",
    "\n",
    "user_item_2_score = sio.mmread(temp_data + \"user_item_2_score\").tocsr()\n",
    "\n",
    "n_users = len(users_index)\n",
    "n_items = len(items_index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<787x800 sparse matrix of type '<class 'numpy.float64'>'\n",
       "\twith 20164 stored elements in Compressed Sparse Row format>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_item_2_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 14.10714286,  22.57894737,   3.38461538,   5.35      ,\n",
       "        13.85714286,  30.625     ,   3.56626506,   4.6       ,\n",
       "         6.26086957,   5.71428571,   1.875     ,   4.46846847,\n",
       "         1.83529412,   5.58730159,   8.68421053,   6.13888889,\n",
       "        13.88888889,   2.59016393,   2.29545455,   2.52941176,\n",
       "         3.77419355,   4.24848485,   6.4375    ,   2.84615385,\n",
       "         6.23134328,  12.42982456,   6.65662651,   2.40350877,\n",
       "        26.5       ,  11.07692308,   3.67567568,  15.6875    ,\n",
       "         8.53448276,   6.4137931 ,   4.29885057,   8.74842767,\n",
       "         1.16783217,   4.57142857,   5.93630573,   7.04132231,\n",
       "         4.04123711,   1.6       ,   2.79032258,  48.2       ,\n",
       "         3.6969697 ,   2.20408163,  14.25      ,   5.43589744,\n",
       "         3.95238095,   9.13333333,   4.27071823,   4.46666667,\n",
       "         3.82608696,   2.36363636,   2.97560976,   3.82978723,\n",
       "        41.125     ,   4.49019608,   6.05607477,   4.04166667,\n",
       "         6.74193548,   4.30985915,   0.73      ,  23.4       ,\n",
       "         4.75490196,   7.3982684 ,   4.64285714,   8.69444444,\n",
       "        43.75      ,   5.84375   ,   9.75862069,   2.72881356,\n",
       "         4.20289855,   1.55      ,   2.55384615,  19.04347826,\n",
       "         6.37837838,   5.04716981,   4.33333333,   4.19101124,\n",
       "         1.15789474,   6.925     ,   6.00699301,   2.425     ,\n",
       "        13.16666667,   5.83221477,   4.46031746,  26.85      ,\n",
       "        14.13333333,   9.80530973,   2.59813084,   6.46060606,\n",
       "         8.73295455,   4.21428571,   2.18095238,   7.28888889,\n",
       "         3.87209302,   7.05952381,   6.02439024,  14.01265823,\n",
       "         8.15217391,  28.40740741,  17.9       ,  12.61904762,\n",
       "         7.41791045,   3.06818182,   9.24378109,   4.56666667,\n",
       "         1.61538462,   8.72881356,  12.15384615,   1.97142857,\n",
       "         4.61904762,  11.61538462,   1.49586777,  13.7       ,\n",
       "         0.76190476,   3.23684211,   5.10344828,   5.23529412,\n",
       "        15.435     ,   4.59836066,   2.0125    ,   4.85714286,\n",
       "         3.69014085,   9.8125    ,   2.86466165,  20.70588235,\n",
       "         6.59360731,  18.89705882,  16.55172414,   2.98795181,\n",
       "         0.80434783,   6.4       ,  13.76470588,  14.05813953,\n",
       "         3.55932203,   1.5625    ,   5.        ,   4.63829787,\n",
       "         4.44680851,   2.04761905,   8.56896552,   4.23255814,\n",
       "         8.21311475,   3.        ,   2.76      ,   2.01639344,\n",
       "        34.83333333,   3.86440678,  35.42857143,   2.44444444,\n",
       "        38.84615385,   5.75      ,   7.7816092 ,   4.65217391,\n",
       "         4.72972973,   7.2962963 ,   2.52631579,   6.5       ,\n",
       "        10.54545455,   3.95238095,   2.06666667,   3.42553191,\n",
       "         3.13559322,   6.5862069 ,  13.51351351,  10.13333333,\n",
       "         3.10416667,   7.58333333,  12.03636364,   8.38571429,\n",
       "         4.73684211,   7.7       ,  15.2       ,  19.58333333,\n",
       "         4.17307692,   1.19672131,   3.12328767,   1.69230769,\n",
       "        20.36734694,  25.        ,  11.09090909,   5.5       ,\n",
       "         3.84090909,   8.07894737,   5.14705882,   2.78313253,\n",
       "        40.83333333,   3.97272727,   4.5042735 ,   1.28205128,\n",
       "         3.82142857,   4.77419355,   2.10344828,   3.09677419,\n",
       "         5.86885246,  16.15      ,   1.1125    ,   2.55384615,\n",
       "         3.40540541,   0.8       ,   8.        ,   9.07142857,\n",
       "         3.28571429,  35.22222222,   6.01282051,   4.80645161,\n",
       "         2.21212121,   9.36842105,   4.46376812,   4.08695652,\n",
       "        15.2173913 ,   5.65      ,   6.63636364,   5.5       ,\n",
       "         2.85      ,   3.32075472,   8.07142857,   3.06153846,\n",
       "         4.95454545,  16.84      ,   5.5       ,   7.32653061,\n",
       "         5.15454545,   0.34883721,  13.51515152,   6.57142857,\n",
       "         5.05479452,  11.39285714,   9.69230769,  14.48648649,\n",
       "         2.77272727,   0.22222222,   7.25581395,   9.12857143,\n",
       "         3.01136364,   3.44      ,   5.89230769,  11.47619048,\n",
       "         2.96491228,   2.4137931 ,  17.96428571,   8.96428571,\n",
       "         8.54054054,   2.62068966,   4.77564103,  15.60869565,\n",
       "         9.53488372,   2.375     ,   5.70588235,   6.4137931 ,\n",
       "        13.76470588,   3.35      ,   1.03603604,   0.        ,\n",
       "         8.42424242,   6.05555556,   4.12631579,  31.08      ,\n",
       "         6.17857143,   1.79591837,   3.42857143,   4.48076923,\n",
       "         4.59259259,   4.48648649,  20.26923077,   3.        ,\n",
       "         8.43333333,  11.4       ,   4.83      ,   8.47826087,\n",
       "         8.5       ,   0.60606061,   1.56756757,   2.75      ,\n",
       "         4.16853933,  15.71794872,  10.27631579,   3.112     ,\n",
       "         5.74074074,   6.51912568,  10.5       ,   5.43529412,\n",
       "         1.14583333,   6.71428571,   1.49152542,  15.35      ,\n",
       "         6.3       ,   3.4       ,  48.55555556,  37.1875    ,\n",
       "        91.44444444,  11.66666667,   5.85714286,   1.24324324,\n",
       "        13.66666667,   9.47826087,   8.62162162,   3.06976744,\n",
       "        17.4       ,  10.84615385,  32.6       ,   4.45714286,\n",
       "         5.10344828,   1.82608696,   9.37735849,  36.2       ,\n",
       "         4.44444444,   5.92105263,   5.38636364,   2.83018868,\n",
       "         6.36065574,  20.71428571,   6.97014925,  28.8125    ,\n",
       "         6.28378378,   3.7       ,   3.78571429,  14.7       ,\n",
       "         9.4       ,  23.86554622,   3.57894737,   2.41463415,\n",
       "         6.82608696,   4.47222222,   8.8       ,   8.25974026,\n",
       "         6.69230769,   6.76923077,  17.12244898,   2.75757576,\n",
       "        10.24      ,   4.28571429,   3.04545455,  10.90277778,\n",
       "         4.56896552,  14.6       ,   1.25      ,   2.70833333,\n",
       "         9.09090909,  31.66666667,   8.50943396,   1.12048193,\n",
       "         2.36956522,  19.36363636,  37.33333333,   5.88888889,\n",
       "         1.5       ,  11.24      ,   6.8125    ,   4.76576577,\n",
       "         9.        ,   7.73529412,   5.10638298,   5.5       ,\n",
       "        13.72727273,   1.72340426,   2.72727273,  12.76190476,\n",
       "         8.96      ,  13.71111111,   5.01923077,   3.48484848,\n",
       "         8.85714286,   1.890625  ,   2.58823529,   3.36363636,\n",
       "         2.46835443,  13.90243902,   5.62337662,   4.84507042,\n",
       "         4.85365854,   1.47916667,   3.5       ,   2.875     ,\n",
       "         5.23648649,   9.17647059,  10.57142857,  14.30769231,\n",
       "        13.65517241,   5.39622642,   2.27272727,  11.625     ,\n",
       "        27.39130435,  24.        ,  12.71428571,   4.72727273,\n",
       "        15.66666667,  11.79545455,   1.58823529,  61.58333333,\n",
       "         5.11764706,   6.        ,   2.98305085,  10.76315789,\n",
       "        16.78378378,   3.        ,  11.67857143,   1.28571429,\n",
       "         5.4       ,   1.43478261,   8.8       ,   8.43333333,\n",
       "        10.42857143,  20.4375    ,  10.96721311,  12.04347826,\n",
       "        22.18181818,   2.6       ,   8.23255814,  21.75      ,\n",
       "         2.61111111,   6.5       ,   8.3       ,   2.94029851,\n",
       "        21.33333333,   0.13333333,   1.45794393,  34.52941176,\n",
       "        42.05263158,   4.37254902,   1.35714286,  10.52941176,\n",
       "         5.86363636,   4.28169014,   0.24137931,   5.46031746,\n",
       "         6.1641791 ,   6.13333333,   4.89473684,   5.        ,\n",
       "         6.83333333,  19.58333333,   2.73529412,   7.88235294,\n",
       "        20.94736842,  17.26315789,  14.        ,   3.52830189,\n",
       "         0.69230769,   2.17391304,   3.48101266,   3.15217391,\n",
       "         4.21875   ,  16.1875    ,   3.08333333,  12.16666667,\n",
       "        12.44444444,   5.04081633,   6.75      ,   3.77272727,\n",
       "        29.36363636,  14.46666667,   5.81818182,   0.89655172,\n",
       "         2.22222222,   6.17391304,   6.22033898,   5.1025641 ,\n",
       "         1.76470588,  18.66666667,   3.47368421,   5.7962963 ,\n",
       "         3.6969697 ,  18.75      ,  12.02439024,  73.66666667,\n",
       "         5.2       ,   2.58333333,  10.15789474,  11.2173913 ,\n",
       "        16.        ,   3.7       ,   1.6969697 ,   6.85185185,\n",
       "         9.25      ,   5.23809524,   5.17391304,  13.44230769,\n",
       "        63.77777778,   8.17391304,   5.95348837,   9.27272727,\n",
       "        18.26666667,  16.26923077,  27.34782609,   2.2       ,\n",
       "        16.28571429,  14.17647059,   3.60606061,   1.26666667,\n",
       "        13.52380952,  10.875     ,  10.11764706,  31.88888889,\n",
       "         2.84615385,  11.89473684,   1.60784314,   2.16666667,\n",
       "        13.5       ,  16.4       ,   5.25      ,   8.5       ,\n",
       "        33.85714286,  13.80952381,  25.09090909,  29.08333333,\n",
       "        16.71428571,  25.05      ,  52.8       ,   4.35      ,\n",
       "        10.45454545,   4.83333333,   1.29166667,   2.21428571,\n",
       "         6.34      ,   2.16071429,   4.46153846,   4.9375    ,\n",
       "         5.5       ,  23.08333333,   1.18181818,   6.22058824,\n",
       "        18.53125   ,  10.1875    ,  15.17647059,   4.57142857,\n",
       "         8.14285714,   2.95652174,  39.875     ,  20.40909091,\n",
       "         6.94117647,   8.17073171,  11.30769231,   7.86956522,\n",
       "         5.01785714,   2.85      ,   4.31034483,   6.43137255,\n",
       "         4.81132075,  25.44444444,  14.34482759,  22.        ,\n",
       "         2.875     ,   1.91666667,  13.375     ,   1.67741935,\n",
       "         1.66666667,  10.33333333,  11.54545455,   9.93333333,\n",
       "        16.65217391,  10.75675676,  12.5       ,  17.04166667,\n",
       "         2.02325581,   3.44705882,   7.3       ,  20.07692308,\n",
       "         0.33333333,  10.41791045,  43.8       ,   8.82352941,\n",
       "         5.        ,   9.12121212,   4.16666667,  11.        ,\n",
       "         7.10344828,   9.5       ,  43.3125    ,   0.6       ,\n",
       "         4.44444444,  14.        ,  18.55      ,  21.14285714,\n",
       "        11.86206897,  11.75      ,  26.        ,  13.8       ,\n",
       "        14.92857143,  14.        ,  34.66666667,   5.        ,\n",
       "        11.25      ,  26.70588235,  15.04761905,  11.57142857,\n",
       "         7.16666667,   8.26666667,   0.5       ,   2.88461538,\n",
       "        22.83333333,  15.        ,   7.32142857,   0.875     ,\n",
       "         6.83333333,  95.        ,   7.26666667,   9.73913043,\n",
       "        19.33333333,   2.14285714,  40.        ,   2.625     ,\n",
       "        35.33333333,   3.45454545,   5.75      ,   7.03225806,\n",
       "        12.        ,  10.57894737,   6.69565217,  41.23529412,\n",
       "        19.5       ,   4.07142857,  11.75      ,  44.8       ,\n",
       "        17.54545455,  49.16666667,  19.2173913 ,  47.2       ,\n",
       "         3.0625    ,  68.        ,   4.46428571,   4.96428571,\n",
       "         4.3125    ,   8.61111111,  47.84210526,  36.5       ,\n",
       "        42.6       ,  13.72222222,   8.25      ,   2.52941176,\n",
       "        14.        ,   9.69565217,   3.25      ,  36.71428571,\n",
       "        14.11111111,   5.09090909,   6.66666667,  15.71428571,\n",
       "        21.        ,  17.2       ,   3.95238095,  11.75      ,\n",
       "        14.        ,   9.55555556,  14.90909091,  16.        ,\n",
       "         1.875     ,   2.81818182,  26.2       ,  28.42857143,\n",
       "        26.15384615,  11.28125   ,   5.39473684,  15.95      ,\n",
       "        15.46153846,   9.        ,   9.78571429,  17.125     ,\n",
       "         3.48148148,   6.66666667,   5.6       ,   5.        ,\n",
       "         3.4       ,  16.77777778,  52.        ,  11.        ,\n",
       "        38.33333333,   5.        ,  11.41666667,  46.875     ,\n",
       "        23.75      ,   1.        ,   1.25      ,   9.44230769,\n",
       "         5.23076923,  13.        ,  51.375     ,   0.        ,\n",
       "         7.88888889,  12.88888889,   2.96666667,   7.375     ,\n",
       "         1.25      ,   1.9       ,   2.25      ,  16.6       ,\n",
       "        11.16666667,  43.75      ,   3.3       ,   6.        ,\n",
       "        29.14285714,  43.        ,  21.16666667,   8.33333333,\n",
       "         6.28571429,  22.66666667,   3.16666667,  13.23529412,\n",
       "        34.33333333,  13.13333333,  26.        ,  17.        ,\n",
       "        19.23529412,   9.33333333,  41.33333333,   7.33333333,\n",
       "        26.        ,  25.77777778,   6.5       ,  34.8       ,\n",
       "         8.7       ,  29.25      ,   6.        ,  14.8       ,\n",
       "         2.        ,  15.93333333,  28.33333333,  44.83333333,\n",
       "         8.25      ,   1.        ,   2.8       ,  83.4       ,\n",
       "        18.66666667,  65.66666667,  41.14285714,  58.6       ,\n",
       "        54.        ,  36.66666667,  24.5       ,  46.        ,\n",
       "        16.33333333,  27.75      ,   7.3       ,  24.25      ,\n",
       "        50.66666667,  50.58333333,  29.33333333,  15.08333333,\n",
       "        50.        ,  39.2       ,  15.2       ,   3.38461538,\n",
       "        13.75      ,  13.5       ,   2.25      ,  67.28571429,\n",
       "        66.66666667,   0.4       ,  13.        ,  10.25      ,\n",
       "         4.71428571,  14.2       ,  38.2       ,  49.        ,\n",
       "        17.33333333,   1.83333333,  77.6       ,  50.        ,\n",
       "        43.        ,  48.        ,   0.        ,  30.75      ,\n",
       "         6.33333333,  21.44444444,   4.5       ,  20.2       ,\n",
       "         3.        , 100.        ,  12.1       ,  24.        ,\n",
       "        26.        ,  20.        ,  13.5       ,   5.2       ,\n",
       "        75.75      ,  22.83333333,   3.        ,  28.        ,\n",
       "         0.        ,  14.        , 100.        ,  20.        ,\n",
       "         5.        , 100.        ,   0.        ])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算每个user的平均打分\n",
    "users_mean_score = np.zeros(n_users)\n",
    "for user_index in range(n_users):\n",
    "    acc = 0.0\n",
    "    cur_user_items_count = 0\n",
    "    for item_index in user_2_items[user_index]:\n",
    "        acc += user_item_2_score[user_index, item_index]\n",
    "        cur_user_items_count += 1\n",
    "    users_mean_score[user_index] = acc/cur_user_items_count\n",
    "users_mean_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cal_sim(user_index_0, user_index_1):\n",
    "    #算出两user共同打过分的item_index集合\n",
    "    if user_index_0 == user_index_1:#同一个user\n",
    "        return 1.0\n",
    "    \n",
    "    both_items = {}\n",
    "    for tmp_item_index in user_2_items[user_index_0]:\n",
    "        if tmp_item_index in user_2_items[user_index_1]:\n",
    "            both_items[tmp_item_index] = 1\n",
    "            \n",
    "    if len(both_items) == 0:\n",
    "        return 0.0 #两者没有相似度\n",
    "    \n",
    "    s0 = np.array([user_item_2_score[user_index_0, tmp_item_index] - users_mean_score[user_index_0] for tmp_item_index in both_items])\n",
    "    s1 = np.array([user_item_2_score[user_index_1, tmp_item_index] - users_mean_score[user_index_1] for tmp_item_index in both_items])\n",
    "    \n",
    "    similarity = 1 - ssd.cosine(s0, s1) \n",
    "    \n",
    "    if np.isnan(similarity): #s1或s2的l2模为0（全部等于该用户的平均打分）\n",
    "        similarity = 0.0\n",
    "    return similarity  \n",
    "\n",
    "#对目标用户user_index对目标item item_index进行估分\n",
    "def user_cf_pred(user_index, item_index):\n",
    "    for tmp_user_index in item_2_users[item_index]:\n",
    "        if user_index == tmp_user_index:\n",
    "            return user_item_2_score[user_index, item_index] #已经打过分了,不用估计了\n",
    "        \n",
    "    sim_abs_sum = 0.0\n",
    "    acc = 0.0\n",
    "    #对目标item打过分的所有user\n",
    "    for tmp_user_index in item_2_users[item_index]:\n",
    "        sim = cal_sim(user_index, tmp_user_index)\n",
    "        if sim != 0:\n",
    "            acc += sim * (user_item_2_score[tmp_user_index, item_index] - users_mean_score[tmp_user_index])\n",
    "            sim_abs_sum = np.abs(sim)\n",
    "        \n",
    "    if sim_abs_sum != 0:\n",
    "        pred_score = users_mean_score[user_index] + acc/sim_abs_sum\n",
    "    else:\n",
    "        pred_score = users_mean_score[user_index] #没有找到相似的用户，返回个默认值\n",
    "    \n",
    "    return pred_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def recommend(tar_user):\n",
    "    tar_user_index = users_index[tar_user]\n",
    "    #训练集中该用户打过分的item\n",
    "    tar_user_items = user_2_items[tar_user_index]\n",
    "    #该用户对所有item的打分\n",
    "    tar_user_items_pred_scores = np.zeros(n_items)\n",
    "    \n",
    "    #预测打分\n",
    "    for i in range(n_items):\n",
    "        if i not in tar_user_items:\n",
    "            tar_user_items_pred_scores[i] = user_cf_pred(tar_user_index, i)\n",
    "    \n",
    "    #推荐\n",
    "    sort_index = sorted(((e,i) for i,e in enumerate(list(tar_user_items_pred_scores))), reverse=True)\n",
    "    columns = ['item', 'score']\n",
    "    df = pd.DataFrame(columns=columns)\n",
    "    \n",
    "    for i in range(0,len(sort_index)):\n",
    "        cur_item_index = sort_index[i][1] \n",
    "        cur_item = list (items_index.keys()) [list (items_index.values()).index (cur_item_index)]\n",
    "        \n",
    "        if ~np.isnan(sort_index[i][0]) and cur_item_index not in tar_user_items:\n",
    "            df.loc[len(df)]=[cur_item, sort_index[i][0]]\n",
    "            \n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "#读取测试数据\n",
    "triplet_cols = ['user','item', 'score'] \n",
    "\n",
    "df_triplet_test = pd.read_csv(temp_data +'triplet_dataset_test.csv', sep=',', names=triplet_cols, encoding='latin-1', header=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\main\\local\\Anaconda3\\envs\\tf1.14\\lib\\site-packages\\scipy\\spatial\\distance.py:720: RuntimeWarning: invalid value encountered in double_scalars\n",
      "  dist = 1.0 - uv / np.sqrt(uu * vv)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "complete:3325fe1d8da7b13dd42004ede8011ce3d7cd205d progress:0.0013774104683195593\n",
      "complete:e82b3380f770c78f8f067f464941057c798eaca2 progress:0.0027548209366391185\n",
      "complete:bdfca47d03157d26f1404075172128a6f8a3d39e progress:0.004132231404958678\n",
      "complete:7ffc14a55b6256c9fa73fc5c5761d210deb7f738 progress:0.005509641873278237\n",
      "complete:083a2a59603a605275107c00812a811526c2a0af progress:0.006887052341597796\n",
      "complete:6c6289326f70321f2b3e072daa44819efc55639a progress:0.008264462809917356\n",
      "complete:eb69f5a5465388b63fe66a410f7c58e17fb7bade progress:0.009641873278236915\n",
      "complete:bd3478df2f64daf2e38b0550c988157b2c118481 progress:0.011019283746556474\n",
      "complete:b4c94d72b15d3c311c10045a58b31f95d9d12785 progress:0.012396694214876033\n",
      "complete:d0a2c5ac5ce1bc3573224d910fe3adfb85d4ee3f progress:0.013774104683195593\n",
      "complete:42060428606a89dafa75e9311b8e77859766fcb0 progress:0.015151515151515152\n",
      "complete:44bf2bb66daf59a20d7b7df64fda29ec16c74cb9 progress:0.01652892561983471\n",
      "complete:a4ccc36714975978b545e35db83584fa9f7fa6c6 progress:0.01790633608815427\n",
      "complete:1a43a7b1bf9a79e34aa16694ebe298454b53a3e6 progress:0.01928374655647383\n",
      "complete:25f83a09541073b1bde62e6e513cb938d3b148de progress:0.02066115702479339\n",
      "complete:c0ff0f1c93f67c1fb372b36b1b08bb4c76bead7d progress:0.02203856749311295\n",
      "complete:15e7692a0539b165bbe0dad78dea78e2ba5fb37f progress:0.023415977961432508\n",
      "complete:dbb235f944fe78ef8198950e64e4bbc2bdbef172 progress:0.024793388429752067\n",
      "complete:281deab3afccc906251ef67a8eda2b9f9baec459 progress:0.026170798898071626\n",
      "complete:104bcda48463a99997f668b897c32234793cd514 progress:0.027548209366391185\n",
      "complete:edbeebbe98aeceb8d38b1de33ac1b1e201041284 progress:0.028925619834710745\n",
      "complete:797cbbace429fee2717a14ff992df0acd3990368 progress:0.030303030303030304\n",
      "complete:726da71c2c2ea119119a7957517fccd028d1be76 progress:0.03168044077134986\n",
      "complete:3bcd2749896db47dec4ea21cbca3494e3c5560c1 progress:0.03305785123966942\n",
      "complete:6c2baa9bfea08ed197d38b9d1606aa5ca57e11ce progress:0.03443526170798898\n",
      "complete:c20bccecc9d79ae108226e36cec975cd3c1227f7 progress:0.03581267217630854\n",
      "complete:934438129c278fe6bb3de6a3275dba13891674ab progress:0.0371900826446281\n",
      "complete:b80501c77662b97dcf29488faa7ff154a40c8ce6 progress:0.03856749311294766\n",
      "complete:ab9a997a846341347a3b7a5ba16a049e8e7d2f44 progress:0.03994490358126722\n",
      "complete:b4357696b37950c23ce4e87f7e8620071f65fc44 progress:0.04132231404958678\n",
      "complete:6326095256b355d4b4fa34771b7b524a1c4cc8f8 progress:0.04269972451790634\n",
      "complete:a41d3edbc2798b6800fe15845a979150eb244b85 progress:0.0440771349862259\n",
      "complete:44ec3ce6ed442ea08e7872cba62489f5b0ed08ec progress:0.045454545454545456\n",
      "complete:aabbd8b9388076451e70846a86cd4c8cb426873f progress:0.046831955922865015\n",
      "complete:1a6916b63039c3c56c62de345c284c11a075eb96 progress:0.048209366391184574\n",
      "complete:20ecc83aa91f1aa3f08ec0e7c21777f8d85ee7e5 progress:0.049586776859504134\n",
      "complete:b6d8573109462bad5457196b31564c4df4235741 progress:0.05096418732782369\n",
      "complete:5bd7cd97b85c91408ef0134a3f7b15d84e01a24c progress:0.05234159779614325\n",
      "complete:189f3d247c01e8319458d82ed68cbaf3112fe9fb progress:0.05371900826446281\n",
      "complete:d2423c01882062ac950909b0ffe96b30e54ab063 progress:0.05509641873278237\n",
      "complete:912a348c57c5b6004f12cfed976ee4569daefb1c progress:0.05647382920110193\n",
      "complete:5996aa0d1a973619e5a950c7f9b7f0132e0f865c progress:0.05785123966942149\n",
      "complete:d964fc033291078031d117ed10adfb615948256d progress:0.05922865013774105\n",
      "complete:690ab317e29d08acb0a11e82eb0f83428cf812f9 progress:0.06060606060606061\n",
      "complete:35b1d8452b62242a2ded2dc5ea05a0a407023cf7 progress:0.06198347107438017\n",
      "complete:755e9959e1c7efb6893b537d54d9df0ba6447e75 progress:0.06336088154269973\n",
      "complete:b21e1b6b14b7b3b8b8e683e82ede0e59ad64e9f7 progress:0.06473829201101929\n",
      "complete:3a34be849c7377037ecb43666ca8aa5988197128 progress:0.06611570247933884\n",
      "complete:865915b55b4b6d355c04cca32cacf107f581dc1c progress:0.0674931129476584\n",
      "complete:bb0c93f971ee41a7bdfedcd9aad701bfadfca5c8 progress:0.06887052341597796\n",
      "complete:738759001498928d8dcb054cd53a1a0cfc200d36 progress:0.07024793388429752\n",
      "complete:4dac0f02b38f49be89ade787020308053fa7f5b7 progress:0.07162534435261708\n",
      "complete:4552b85a1b315556ad50d4a10942a3e86fc7d72c progress:0.07300275482093664\n",
      "complete:7ce93f17fcdfe671e20526527eaf2a4812462171 progress:0.0743801652892562\n",
      "complete:b048f21afd5e7467f187bf9f9d413e97c32313a9 progress:0.07575757575757576\n",
      "complete:dd29009e6148c8c21544c27e7c074b4601ba54e5 progress:0.07713498622589532\n",
      "complete:b0a3d755fe4a6549e241676b0a388e9410749c05 progress:0.07851239669421488\n",
      "complete:97dd000ec4ace34cb5b3d3a47226b8c905bbe0cd progress:0.07988980716253444\n",
      "complete:aff2b00aeba4a389d22c474dc33645e0a6dfd56e progress:0.081267217630854\n",
      "complete:f629b337d01f254e0d685ccc66ec2fd5ddba78d9 progress:0.08264462809917356\n",
      "complete:a2679496cd0af9779a92a13ff7c6af5c81ea8c7b progress:0.08402203856749312\n",
      "complete:fc77d71ecc8a4c7f4a0402fbe9118973124391fe progress:0.08539944903581267\n",
      "complete:9b06094a34c6c421b86df33c879b8980d1038cd8 progress:0.08677685950413223\n",
      "complete:eaa2b3c9e086a662ab15e10ca6211a3207f40a50 progress:0.0881542699724518\n",
      "complete:82cd7de99ecda208dcd18346892859f35daf0520 progress:0.08953168044077135\n",
      "complete:c2fd42c798c8a62d997896641e4274b17cc252f0 progress:0.09090909090909091\n",
      "complete:7560076aa3ff4c9a46d917262a87a3d830543469 progress:0.09228650137741047\n",
      "complete:613081f27b9917e4d0e84639fc02661d809f36e3 progress:0.09366391184573003\n",
      "complete:40512c7d4dbe8126c88a794a8bc7bf094b401d64 progress:0.09504132231404959\n",
      "complete:c1255748c06ee3f6440c51c439446886c7807095 progress:0.09641873278236915\n",
      "complete:e6b18cffa2afad9245b7d9eb08390efeebef1f3b progress:0.09779614325068871\n",
      "complete:3288389bf9ef956a23a0a4ea86f60bf24ba7f69e progress:0.09917355371900827\n",
      "complete:0252acdea2a493da2704c23eebaeaa155b18b7d0 progress:0.10055096418732783\n",
      "complete:956dc1095d8f22575d3936191ce20b789b0ffc4d progress:0.10192837465564739\n",
      "complete:fc05f377863a77d7784b02de2cc06cdecb85968b progress:0.10330578512396695\n",
      "complete:54d07c4eff369f457c7a5d1c0b9b6c48feb949f4 progress:0.1046831955922865\n",
      "complete:bc3777585de3e0786e06328c118de9eb47470726 progress:0.10606060606060606\n",
      "complete:ca658ff2092285a3fd7f1ccbc54e10211e184f66 progress:0.10743801652892562\n",
      "complete:4403518f697ae745538e83317ed66b0f5b09a356 progress:0.10881542699724518\n",
      "complete:e6e0f68e948d7bcbf2ed9c4506a40a139a5e7bc7 progress:0.11019283746556474\n",
      "complete:aba3117aae6344fb8d28c711b87b2aa3f1c4be75 progress:0.1115702479338843\n",
      "complete:491d048e26c51fcda0744355bf191d4ccf36f118 progress:0.11294765840220386\n",
      "complete:cbc7bddbe3b2f59fdbe031b3c8d0db4175d361e6 progress:0.11432506887052342\n",
      "complete:e0892dd1ac13bb10434b026372e7a0d2fae62444 progress:0.11570247933884298\n",
      "complete:6a944bfe30ae8d6b873139e8305ae131f1607d5f progress:0.11707988980716254\n",
      "complete:7aea7fc1f48e8f32b403113baec0c0a8c6086ce4 progress:0.1184573002754821\n",
      "complete:261ea96dea056f44efc267d6fbc5718240aa82b0 progress:0.11983471074380166\n",
      "complete:2e259fa6ebcc8b4df0ee93b8a9b94f3dce8b4272 progress:0.12121212121212122\n",
      "complete:d331a8bf7d0ca9cb37e375496e6075603f6fb44a progress:0.12258953168044077\n",
      "complete:0b19fe0fad7ca85693846f7dad047c449784647e progress:0.12396694214876033\n",
      "complete:b7032f457c624e23113c39b2f9c444a961c3fdf8 progress:0.12534435261707988\n",
      "complete:fe8b98246d279f71f7cb0d493cdedce2bbc30aae progress:0.12672176308539945\n",
      "complete:57107a3feced24c067db22bce2d59c02d53ad04e progress:0.128099173553719\n",
      "complete:8ec7fd0c1acf1dbe44720e5eab44dbe524eb6caf progress:0.12947658402203857\n",
      "complete:ec6dfcf19485cb011e0b22637075037aae34cf26 progress:0.13085399449035812\n",
      "complete:7e543508a213f4f22e0cb54ecf2df9c370070a28 progress:0.1322314049586777\n",
      "complete:a05e548059abb1f77cad6cb9c3c0c48e0616f551 progress:0.13360881542699724\n",
      "complete:4d98756ff69be79de228c15432245766d4bf0316 progress:0.1349862258953168\n",
      "complete:96f7b4f800cafef33eae71a6bc44f7139f63cd7a progress:0.13636363636363635\n",
      "complete:119b7c88d58d0c6eb051365c103da5caf817bea6 progress:0.13774104683195593\n",
      "complete:83b94f1d1bf5581499cc0738807c8c41f3bf6706 progress:0.13911845730027547\n",
      "complete:33a1286454a3cff06e3c2324be746d2e23d7c270 progress:0.14049586776859505\n",
      "complete:2a3d80f37b92fa113d4f4b0785d797153cca5f63 progress:0.1418732782369146\n",
      "complete:b33890c07bdc7497718af93cd00077e644440b49 progress:0.14325068870523416\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "complete:67f2478f4356a9be264521f64b56db8de22bc9f5 progress:0.1446280991735537\n",
      "complete:20ad98ab543da9ec41c6ac3b6354c5ab3ca6bc5e progress:0.14600550964187328\n",
      "complete:4cf9722427d38047fe018ff035305f2ff3398dee progress:0.14738292011019283\n",
      "complete:91cf50e35e3fa924450d86ecae248b7cea113f0b progress:0.1487603305785124\n",
      "complete:debd74c1999b195eeddf09bc0af751c93a52c205 progress:0.15013774104683195\n",
      "complete:062eef2a03b53d2b10f5018135e3361659c6a3bf progress:0.15151515151515152\n",
      "complete:ea01cb794fb07af9cb3a29da0bd31126735be410 progress:0.15289256198347106\n",
      "complete:ee30810179c611d32705fe0b71333dcb8703b30a progress:0.15426997245179064\n",
      "complete:bf82fdc9210bdaa712a5e310a35fe9784d5700e4 progress:0.15564738292011018\n",
      "complete:e3fce0e4f316567685119c9af7d04d67c84f0a46 progress:0.15702479338842976\n",
      "complete:138d73356c23e6e12aa82fb5dc9225428c196464 progress:0.1584022038567493\n",
      "complete:25f202e6e02803d3e2f19fea3f88293e24483196 progress:0.15977961432506887\n",
      "complete:dae0bbb6e964a8f5495fa6edfb7321437b28b5a1 progress:0.16115702479338842\n",
      "complete:2b8bfcdc258cdfe9dce9991d67786b3cebbc3b22 progress:0.162534435261708\n",
      "complete:cc60505d5994ddcd6103a2bf1faf5f774b7909d3 progress:0.16391184573002754\n",
      "complete:4e73d9e058d2b1f2dba9c1fe4a8f416f9f58364f progress:0.1652892561983471\n",
      "complete:c11dea7d1f4d227b98c5f2a79561bf76884fcf10 progress:0.16666666666666666\n",
      "complete:17d3104690880b6fbc4e347414c3999fa5df155a progress:0.16804407713498623\n",
      "complete:f6ae5e682750e815c1709ca99138d03b039839d6 progress:0.16942148760330578\n",
      "complete:f2a03543373cfed80f076f3337360630f084ad30 progress:0.17079889807162535\n",
      "complete:b5186694bb2406962edb3a8042b535449627fa37 progress:0.1721763085399449\n",
      "complete:8cb51abc6bf8ea29341cb070fe1e1af5e4c3ffcc progress:0.17355371900826447\n",
      "complete:a18aa09c5b8a1c03d03cdf6d8eb11c2bf5b907cd progress:0.174931129476584\n",
      "complete:45f06d9d15adbd8575a63e848b1f1c202afbf308 progress:0.1763085399449036\n",
      "complete:18c1dd917693fd929e3f99dd7906c2aafe9ff17f progress:0.17768595041322313\n",
      "complete:90d2fcb1dbe47dc1e9442587e259811a0437a13f progress:0.1790633608815427\n",
      "complete:fef771ab021c200187a419f5e55311390f850a50 progress:0.18044077134986225\n",
      "complete:fd1ebc6caa7ad07c84677ba6bada683077bf0f15 progress:0.18181818181818182\n",
      "complete:125878fa33947aaf4a8cdb1fb4843298f31a0ae0 progress:0.18319559228650137\n",
      "complete:b7948bd0aab3932852dce33f89604a85736a449a progress:0.18457300275482094\n",
      "complete:a2758cfd225f99b0494d98b3e7c65920345f95c8 progress:0.1859504132231405\n",
      "complete:23563d3fc5aa322fded41b295e36291473515771 progress:0.18732782369146006\n",
      "complete:2744a71984cdf296fb94de2b9d5aa0f065ffb1ab progress:0.1887052341597796\n",
      "complete:1fbb0f6de552dec6eff67288fbaf3efeae2a2427 progress:0.19008264462809918\n",
      "complete:b7c24f770be6b802805ac0e2106624a517643c17 progress:0.19146005509641872\n",
      "complete:2d1d1596c8d162b33ce8bdb575dae8073afa1086 progress:0.1928374655647383\n",
      "complete:e6558266b59a42a328ae3faac89701cfedabb558 progress:0.19421487603305784\n",
      "complete:9c2dfee26bbdd4fb19e9800244bea6e7181caeae progress:0.19559228650137742\n",
      "complete:fe2d77de7e57f3b3eedcf473545110b13ca03426 progress:0.19696969696969696\n",
      "complete:36bee226881241a38e3c9997cf0c84e2959035e7 progress:0.19834710743801653\n",
      "complete:f99a25251dfd3c44b629c3658bf6c0d0a7a3d0ce progress:0.19972451790633608\n",
      "complete:0d54fad06b250c41865f6af5b8d35dd5c5750c75 progress:0.20110192837465565\n",
      "complete:92d2e8ff105b7b7ddc163e740921cafdbcd815bf progress:0.2024793388429752\n",
      "complete:c1912062175dc4b3ea5a3a0cdb963c704bb9c881 progress:0.20385674931129477\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-9c4c8132878c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     21\u001b[0m     \u001b[0muser_records_test\u001b[0m\u001b[1;33m=\u001b[0m \u001b[0mdf_triplet_test\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mdf_triplet_test\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0muser\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0muser\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     22\u001b[0m     \u001b[1;31m#计算推荐item\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 23\u001b[1;33m     \u001b[0mrec_items\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrecommend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0muser\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     24\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     25\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mRECOMMEND_ITEM_SIZE\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-7-0dee2c803834>\u001b[0m in \u001b[0;36mrecommend\u001b[1;34m(tar_user)\u001b[0m\n\u001b[0;32m     21\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     22\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;33m~\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0misnan\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msort_index\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mcur_item_index\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mtar_user_items\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 23\u001b[1;33m             \u001b[0mdf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mcur_item\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msort_index\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     24\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     25\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\main\\local\\Anaconda3\\envs\\tf1.14\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m__setitem__\u001b[1;34m(self, key, value)\u001b[0m\n\u001b[0;32m    669\u001b[0m             \u001b[0mkey\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply_if_callable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    670\u001b[0m         \u001b[0mindexer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_setitem_indexer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 671\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_setitem_with_indexer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    672\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    673\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_validate_key\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\main\\local\\Anaconda3\\envs\\tf1.14\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_setitem_with_indexer\u001b[1;34m(self, indexer, value)\u001b[0m\n\u001b[0;32m    873\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    874\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mmissing\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 875\u001b[1;33m                 \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_setitem_with_indexer_missing\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    876\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    877\u001b[0m         \u001b[1;31m# set\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\main\\local\\Anaconda3\\envs\\tf1.14\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_setitem_with_indexer_missing\u001b[1;34m(self, indexer, value)\u001b[0m\n\u001b[0;32m   1121\u001b[0m                 \u001b[0mvalue\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mSeries\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mindexer\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1122\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1123\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_data\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_data\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1124\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_maybe_update_cacher\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mclear\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1125\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\main\\local\\Anaconda3\\envs\\tf1.14\\lib\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36mappend\u001b[1;34m(self, other, ignore_index, verify_integrity, sort)\u001b[0m\n\u001b[0;32m   7058\u001b[0m                 \u001b[0mcombined_columns\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobject\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0midx_diff\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   7059\u001b[0m             other = (\n\u001b[1;32m-> 7060\u001b[1;33m                 \u001b[0mother\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcombined_columns\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   7061\u001b[0m                 \u001b[1;33m.\u001b[0m\u001b[0mto_frame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   7062\u001b[0m                 \u001b[1;33m.\u001b[0m\u001b[0mT\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minfer_objects\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\main\\local\\Anaconda3\\envs\\tf1.14\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36minfer_objects\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m   5916\u001b[0m         return self._constructor(\n\u001b[0;32m   5917\u001b[0m             self._data.convert(\n\u001b[1;32m-> 5918\u001b[1;33m                 \u001b[0mdatetime\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnumeric\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtimedelta\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcoerce\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   5919\u001b[0m             )\n\u001b[0;32m   5920\u001b[0m         ).__finalize__(self)\n",
      "\u001b[1;32mE:\\main\\local\\Anaconda3\\envs\\tf1.14\\lib\\site-packages\\pandas\\core\\internals\\managers.py\u001b[0m in \u001b[0;36mconvert\u001b[1;34m(self, **kwargs)\u001b[0m\n\u001b[0;32m    583\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    584\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mconvert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 585\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"convert\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    586\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    587\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mreplace\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\main\\local\\Anaconda3\\envs\\tf1.14\\lib\\site-packages\\pandas\\core\\internals\\managers.py\u001b[0m in \u001b[0;36mapply\u001b[1;34m(self, f, filter, **kwargs)\u001b[0m\n\u001b[0;32m    445\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult_blocks\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    446\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmake_empty\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0maxes\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 447\u001b[1;33m         \u001b[0mbm\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult_blocks\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0maxes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdo_integrity_check\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    448\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mbm\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    449\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\main\\local\\Anaconda3\\envs\\tf1.14\\lib\\site-packages\\pandas\\core\\internals\\managers.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, blocks, axes, do_integrity_check)\u001b[0m\n\u001b[0;32m    141\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_consolidate_check\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    142\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 143\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_rebuild_blknos_and_blklocs\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    144\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    145\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mmake_empty\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxes\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\main\\local\\Anaconda3\\envs\\tf1.14\\lib\\site-packages\\pandas\\core\\internals\\managers.py\u001b[0m in \u001b[0;36m_rebuild_blknos_and_blklocs\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    222\u001b[0m             \u001b[0mrl\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mblk\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmgr_locs\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    223\u001b[0m             \u001b[0mnew_blknos\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mrl\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindexer\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mblkno\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 224\u001b[1;33m             \u001b[0mnew_blklocs\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mrl\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindexer\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrl\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    225\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    226\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mnew_blknos\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0many\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "RECOMMEND_ITEM_SIZE = 10\n",
    "\n",
    "n_hits = 0 #击中数目\n",
    "n_total_rec_items = 0 #推荐的item总数(推荐了一个item就加1)\n",
    "n_test_items = 0 #真实的item记录总数()\n",
    "\n",
    "#所有被推荐商品的集合（对不同用户），用于计算覆盖度\n",
    "all_rec_items = set()\n",
    "\n",
    "#统计总的用户\n",
    "unique_users_test = df_triplet_test['user'].unique()\n",
    "\n",
    "#已经计算完毕的用户数\n",
    "complete_user_count = 0\n",
    "\n",
    "for user in unique_users_test:\n",
    "    if user not in users_index:\n",
    "        print(str(user) + 'is new user,\\n') \n",
    "        continue #此user是新用户，无法计算出相似度\n",
    "    #找出test中user的记录集合\n",
    "    user_records_test= df_triplet_test[df_triplet_test.user == user]\n",
    "    #计算推荐item\n",
    "    rec_items = recommend(user)\n",
    "    \n",
    "    for i in range(RECOMMEND_ITEM_SIZE):\n",
    "        item = rec_items.iloc[i]['item']\n",
    "        \n",
    "        if item in user_records_test['item'].values:\n",
    "            n_hits += 1\n",
    "        all_rec_items.add(item)\n",
    "        \n",
    "    #推荐的item总数\n",
    "    n_total_rec_items += RECOMMEND_ITEM_SIZE\n",
    "    \n",
    "    #真实item的总数\n",
    "    n_test_items += user_records_test.shape[0]   \n",
    "    \n",
    "    complete_user_count += 1\n",
    "    print('complete:' + str(user) + \" progress:\" + str(complete_user_count/len(unique_users_test)))\n",
    "\n",
    "#Precision & Recall\n",
    "precision = n_hits / (1.0*n_total_rec_items)\n",
    "recall = n_hits / (1.0*n_test_items)\n",
    "\n",
    "#覆盖度：推荐商品占总需要推荐商品的比例\n",
    "coverage = len(all_rec_items) / (1.0* n_items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
